//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package mgr;

import SevenZip.Compression.LZMA.Decoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class ResourceManager {
    public ResourceManager() {
    }

    private static ByteArrayOutputStream csvToStream(BufferedInputStream inStream) throws IOException, Exception {
        ByteArrayOutputStream csvas = new ByteArrayOutputStream();
        BufferedOutputStream outStream = new BufferedOutputStream(csvas);
        byte propertiesSize = 5;
        byte[] properties = new byte[propertiesSize];
        if(inStream.read(properties, 0, propertiesSize) != propertiesSize) {
            throw new Exception("input .lzma file is too short");
        } else {
            Decoder decoder = new Decoder();
            if(properties[0] == 93 && properties[3] == 4) {
                decoder.SetDecoderProperties(properties);
                long var10 = 0L;

                for(int i = 0; i < 4; ++i) {
                    int v = inStream.read();
                    if(v < 0) {
                        throw new Exception("Can\'t read stream size");
                    }

                    var10 |= (long)v << 8 * i;
                }

                if(!decoder.Code(inStream, outStream, var10)) {
                    throw new Exception("Error in data stream");
                }
            } else {
                byte[] buf = new byte[inStream.available()];
                int size = inStream.read(buf);
                if(size != buf.length) {
                    throw new Exception("Error in data stream");
                }

                outStream.write(properties);
                outStream.write(buf);
            }

            outStream.flush();
            return csvas;
        }
    }

    public BufferedReader getCsvReader(String game, String cfgFileRef) throws IOException, Exception {
        BufferedInputStream inStream = new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream("csv/" + game + "/" + cfgFileRef));
        ByteArrayOutputStream csvas = csvToStream(inStream);
        return csvas.size() > 0?new BufferedReader(new InputStreamReader(new ByteArrayInputStream(csvas.toByteArray()), "UTF-8")):null;
    }

    public boolean getConfig(String game, String cfgFileRef, LinkedHashMap<String, Integer> header, List<List<String>> entries) throws IOException, Exception {
        BufferedReader inStream = this.getCsvReader(game, cfgFileRef);
        if(inStream == null) {
            return false;
        } else {
            String rds = inStream.readLine().replace("\"", "");
            String[] hdr = rds.split(",");
            rds = inStream.readLine().replace("\"", "");

            for(int types = 0; types < hdr.length; ++types) {
                header.put(hdr[types], Integer.valueOf(types));
            }

            String[] var12 = rds.split(",");
            if(hdr.length != var12.length) {
                throw new IOException("Invalid HD CSV format.");
            } else {
                while((rds = inStream.readLine()) != null) {
                    rds = rds.replace("\"", "");
                    String[] v = rds.split(",");
                    ArrayList vl = new ArrayList();

                    int i;
                    for(i = 0; i < v.length; ++i) {
                        vl.add(v[i]);
                    }

                    while(i < hdr.length) {
                        vl.add("");
                        ++i;
                    }

                    entries.add(vl);
                }

                return true;
            }
        }
    }
}
